const Controller = require('egg').Controller;


class wxType extends Controller {
    async toptype(){
        const {app,ctx,service} = this;

        let redis_data = await app.sessionStore.get('type_list');
        if(!app.lodash.isEmpty(redis_data)){
            ctx.body = {
                code : 200,
                data : redis_data,
                msg : ''
            }
        }else{
            let type = await ctx.model.FdType.findAll({where:{is_parent:'1',deleted:'0'},order:[['num','asc'],['created_at','desc']]});

            await app.sessionStore.set('type_list',type);

            ctx.body = {
                code : 200,
                data : type,
                msg : ''
            }
        }
    }

    async sectype(){
        const {app,ctx,service} = this;
        const {parent} = ctx.query;

        let redis_data = await app.sessionStore.get('typeSecOf_'+parent);
        if(!app.lodash.isEmpty(redis_data)){
            ctx.body = {
                code : 200,
                data : redis_data,
                msg : ''
            }
        }else{
            let type = await ctx.model.FdType.findAll({where:{is_parent:'0',deleted: '0',parent:parent},order:[['num','asc'],['created_at','desc']]});

            await app.sessionStore.set('typeSecOf_'+parent,type);

            ctx.body = {
                code : 200,
                data : type,
                msg : ''
            }
        }
    }
}

module.exports = wxType;
